﻿using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Linq;

namespace Batman.DLL.Todo.Factories
{
    public class TodoDbContextFactory : IDesignTimeDbContextFactory<TodoDbContext>
    {
        public TodoDbContext CreateDbContext(string[] args)
        {
            //var env = "Development";
            var env = "Production";
            IConfigurationRoot configuration = new ConfigurationBuilder()
               .SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile($"appsettings.{env}.json")
               .Build();

            var builder = new DbContextOptionsBuilder<TodoDbContext>();
            var connStr = configuration.GetConnectionString("Default");
            builder.UseMySql(connStr, sql =>
            {
                sql.EnableRetryOnFailure();
                sql.MigrationsAssembly("Batman.DLL.Todo");
            });


            var production = new TodoDbContext(builder.Options);

            return production;
        }
    }
}
